using System.Collections.Generic;
using System.Linq;
using Magiq.Behaviors;
using Magiq.Query;

namespace Magiq.Databases.Query
{
    public class DatabaseQueryStrategy<T> : BehaviorHandler<IDatabaseQueryBehavior<T>>,IQueryStrategy<T>, IDatabaseBasedExecution where T : class
    {
        private readonly IQuery<T> query;

        public DatabaseQueryStrategy(IDatabaseAdapter databaseAdapter, IQuery<T> query) : base(query.Behavior)
        {
            DatabaseAdapter = databaseAdapter;
            this.query = query;
            Default<DatabaseQueryBehavior<T>>();
        }

        public IQueryable<T> Execute()
        {
            Call(x => x.Initialize(DatabaseAdapter, query));
            var databaseQuery = Call(x => x.BuildDatabaseQuery());
            return Call(x => x.GetQueryableFrom(databaseQuery));
        }

        public IDatabaseAdapter DatabaseAdapter { get; private set; }
    }

    public class DatabaseQuery<T> : DatabaseOperation
    {
        public ITypeMapping SourceTypeMapping { get; set; }
        public IEnumerable<T> Source { get; set; }
    }
}
